//
//  CustomCalloutView.swift
//  ShareUmbrella
//
//  Created by keng fu on 2017/8/30.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import SwiftyJSON

class CustomCalloutView: UIView {

    var portraitView = UIImageView()
    var bgView = UIImageView()
    var subtitleLabel = UILabel()
    var titleLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.clear
        self.initSubView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
    
    func initSubView(){
        // 添加图片，即背景图
        self.bgView = UIImageView(frame:.zero)
        self.addSubview(self.bgView)
//        self.bgView.backgroundColor = .white
        self.bgView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self)
        }
        self.bgView.image = UIImage(named: "合并形状")
        
        // 添加图片，即电话图片
       
        // 添加图片，即用户头像
        self.portraitView = UIImageView(frame:.zero).ys.customize({ (view) in
            self.addSubview(view)
            view.snp.makeConstraints { (make) in
                    make.centerY.equalTo(self.snp.centerY).offset(-10)
                    make.leading.equalTo(self.snp.leading).offset(10)
                    make.width.height.equalTo(40)
            }

        })
        
        // 添加标题，即商户名
        self.titleLabel = UILabel(frame:.zero).ys.customize({ (view) in
            self.addSubview(view)
            view.font = UIFont.boldSystemFont(ofSize: 13)
            view.textColor = UIColor(colorLiteralRed: 0x33 / 255.0, green: 0x33 / 255.0, blue: 0x33 / 255.0, alpha: 1)
            view.text = "test title"
            view.snp.makeConstraints { (make) in
                make.top.equalTo(self).offset(10)
                make.leading.equalTo(self.portraitView.snp.trailing).offset(10)
                make.trailing.equalTo(self.snp.trailing).offset(-10)
            }
        })
        // 添加副标题，即商户地址

        self.subtitleLabel = UILabel(frame:.zero).ys.customize({ (view) in
            self.addSubview(view)
            view.font = UIFont.boldSystemFont(ofSize: 11)
            view.textColor = UIColor.lightGray
            view.text = "test title"
            view.snp.makeConstraints { (make) in
                make.bottom.equalTo(self.portraitView)
                make.leading.equalTo(self.portraitView.snp.trailing).offset(10)
                make.trailing.equalTo(self.snp.trailing).offset(-10)
            }
        })

    }
    
    
    func setTitle(title: String) -> Void {
        self.titleLabel.text = title
    }
    
    func setSubTitle(subTitle: String) -> Void {
        self.subtitleLabel.text = subTitle
    }
    
    func setImage(image: UIImage) -> Void {
        self.portraitView.image = image
    }
    
    func callAction(){
        let urlString = "tel://123456"
        if let url = URL(string: urlString) {
            //根据iOS系统版本，分别处理
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:],
                                          completionHandler: {
                                            (success) in
                })
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
}
